/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jhh09wxu.globalgadgets.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.ServerException;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jhh09wxu.globalgadgets.ejb.entity.Customer;
import jhh09wxu.globalgadgets.ejb.session.CustomerSession;

/**
 *
 * @author ranga
 */
public class Register extends HttpServlet {

    List<Customer> custList = null;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServerException, IOException, ServletException {

        response.setContentType("text/html;charset=UTF-8");
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        String country = request.getParameter("country");
        String nickname = request.getParameter("nickname");
        try {

            InitialContext context = new InitialContext();

            CustomerSession custsession = (CustomerSession) context.lookup("globalgadgets_jhh09wxu/CustomerSessionBean/remote");

            custList = custsession.findCustomers(nickname);

            if (custList.isEmpty()) {
                custsession.registerCustomer(nickname, firstname, lastname, address, password, country, null);
                request.setAttribute("msg", "persited");

            } else {
                request.setAttribute("msg", "nickname exists.. enter new one");
                request.setAttribute("list", custList);
            }

        } catch (NamingException ex) {
            request.setAttribute("error", ex);
        }

        RequestDispatcher view = request.getRequestDispatcher("jsps/output.jsp");
        view.forward(request, response);
    }
}
